package cn.edu.xmu.orderpayment.order.model.bo;

import cn.edu.xmu.orderpayment.order.model.po.OrderItemPo;
import cn.edu.xmu.orderpayment.order.model.po.OrderPo;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import cn.edu.xmu.orderpayment.order.model.vo.NewOrderVo;
import cn.edu.xmu.orderpayment.order.model.vo.OrderItemVo;
import lombok.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Chencheng Liao 22920192204238
 * @date 2021/12/11
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order implements Serializable {
    public enum State {
        PENDING_PAYMENT(100, "待付款"),
        NEW_ORDER(101, "新订单"),
        PENDING_FINAL_PAYMENT(102, "待支付尾款"),
        PENDING_DELIVERY(200, "待收货"),
        PAYMENT_COMPLETED(201, "付款完成"),
        PENDING_GROUPING(202, "待成团"),
        SHIPPED(300, "已发货"),
        COMPLETED(400, "已完成"),
        CANCELED(500, "已取消"),
        WAIT_REFUND(501,"待退款"),
        HAS_REFUNDED(502,"已退款");

        private static final Map<Integer, State> stateMap;
        static {
            stateMap = new HashMap<>();
            for (State enum1 : values()) {
                stateMap.put(enum1.code, enum1);
            }
        }


        private int code;
        private String description;

        State(int code, String description) {
            this.code = code;
            this.description = description;
        }

        public Integer getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }
    }

    private Long id;
    private Long customerId;
    private Long shopId;
    private String orderSn;
    private Long pid;
    private String consignee;
    private Long regionId;
    private String address;
    private String mobile;
    private String message;
    private Long advancesaleId;
    private Long grouponId;
    private Long expressFee;
    private Long discountPrice;
    private Long originPrice;
    private Long point;
    private LocalDateTime confirmTime;
    private String shipmentSn;
    private Integer state;
    private Byte beDeleted;
    private Long creatorId;
    private String creatorName;
    private Long modifierId;
    private String modifierName;
    private LocalDateTime gmtCreate;
    private LocalDateTime gmtModified;

    private List<OrderItem> orderItems;



    /**
     * 用po对象构建bo对象
     */
    public Order (OrderPo po, List<OrderItemPo> orderItemPos)
    {

        this.address=po.getAddress();
        if (po.getBeDeleted().intValue()==1) this.beDeleted = 1;
        else this.beDeleted = 0;
        this.confirmTime=po.getConfirmTime();
        this.consignee=po.getConsignee();
        this.customerId=po.getCustomerId();
        this.discountPrice=po.getDiscountPrice();
        this.expressFee=po.getExpressFee();
        this.id=po.getId();
        this.message=po.getMessage();
        this.mobile=po.getMobile();
        this.orderSn=po.getOrderSn();
        this.gmtCreate=po.getGmtCreate();
        this.gmtModified=po.getGmtModified();
        this.originPrice=po.getOriginPrice();
        if(orderItemPos!=null) {
            this.orderItems = new ArrayList<>(orderItemPos.size());
            for (OrderItemPo orderItemPo : orderItemPos) {
                OrderItem orderItem = new OrderItem(orderItemPo);
                this.orderItems.add(orderItem);
            }
        }
        this.pid=po.getPid();
        this.grouponId=po.getGrouponId();
        this.shipmentSn=po.getShipmentSn();
        this.mobile=po.getMobile();
        this.shopId=po.getShopId();
        this.regionId=po.getRegionId();
        this.advancesaleId =po.getAdvancesaleId();
        this.point=po.getPoint();
        this.state=po.getState();
        this.creatorId=po.getCreatorId();
        this.creatorName=po.getCreatorName();
        this.modifierId=po.getModifierId();
        this.modifierName=po.getModifierName();
    }

    /**
     * 用vo对象构建bo对象
     */
    public Order (NewOrderVo vo)
    {
        this.address = vo.getAddress();
        this.consignee = vo.getConsignee();
        this.regionId = vo.getRegionId();
        this.mobile = vo.getMobile();
        this.message = vo.getMessage();
        this.advancesaleId = vo.getAdvanceSaleId();
        this.grouponId = vo.getGrouponId();
        this.point = vo.getPoint();

        this.orderItems = new ArrayList<>();
        for (OrderItemVo orderItem : vo.getOrderItems()) {
            OrderItem newOrderItem = new OrderItem();
            newOrderItem.setProductId(orderItem.getProductId());
            newOrderItem.setOnsaleId(orderItem.getOnsaleId());
            newOrderItem.setQuantity(orderItem.getQuantity());
            newOrderItem.setCouponActivityId(orderItem.getCouponActivityId());
            newOrderItem.setPoint(orderItem.getPoint());
            newOrderItem.setGmtCreate(LocalDateTime.now());
            this.orderItems.add(newOrderItem);
        }
    }

    public void init(){
        this.pid=0L;
        this.orderSn= Common.genSeqNum(1);
        this.expressFee=0L;
        this.discountPrice=0L;
        this.originPrice=0L;
        this.point=0L;
        this.advancesaleId =0L;
        this.grouponId=0L;
        this.setState(State.NEW_ORDER.getCode());
        this.beDeleted=((byte)0);
    }
}
